/**
 * AI API Services
 * AI功能相关API接口
 */
import { get, post, put, del } from './index'

export interface QuestionGenerationRequest {
  subject: string
  topic: string
  difficulty?: 'easy' | 'medium' | 'hard'
  question_type?: 'single_choice' | 'multiple_choice' | 'true_false' | 'short_answer' | 'essay' | 'fill_blank'
  count?: number
  additional_context?: string
}

export interface GradingRequest {
  grading_type: 'essay' | 'short_answer'
  // Essay grading
  essay_content?: string
  essay_prompt?: string
  rubric?: string
  max_score?: number
  // Short answer grading
  student_answer?: string
  reference_answer?: string
  question?: string
}

export interface AnalyticsRequest {
  analysis_type: 'learning_progress' | 'study_recommendations'
  performance_data?: any[]
  subject?: string
  weak_areas?: string[]
}

export interface GeneratedQuestion {
  id: string
  title: string
  content: string
  options: string[]
  correct_answer: string
  explanation: string
  difficulty: string
  estimated_time: number
  score: number
}

export interface GradingResult {
  score: number
  score_percentage: number
  strengths: string[]
  weaknesses: string[]
  detailed_feedback: string
  suggestions: string[]
}

export interface AIUsageStats {
  total_usage: number
  successful_usage: number
  success_rate: number
  monthly_usage: number
  service_breakdown: any[]
  recent_usage: any[]
}

export const aiApi = {
  // AI题目生成
  generateQuestions: (data: QuestionGenerationRequest) =>
    post<any>('/api/ai/generate-questions/', data),

  // 获取AI生成的题目列表
  getGeneratedQuestions: (params?: {
    subject?: string
    difficulty?: string
    question_type?: string
    is_approved?: boolean
  }) => get<any>('/api/ai/questions/', params),

  // AI评分助手
  gradeAnswer: (data: GradingRequest) =>
    post<any>('/api/ai/grade/', data),

  // AI分析服务
  analyzeLearning: (data: AnalyticsRequest) =>
    post<any>('/api/ai/analyze/', data),

  // 获取AI使用统计
  getUsageStats: () =>
    get<AIUsageStats>('/api/ai/usage-stats/'),

  // AI服务状态检查
  getStatus: () =>
    get<any>('/api/ai/status/'),

  // AI服务反馈
  submitFeedback: (data: {
    usage_id?: string
    satisfaction: number
    feedback?: string
  }) => post<any>('/api/ai/feedback/', data),

  // 批准AI生成的题目
  approveQuestion: (questionId: string) =>
    post<any>(`/api/ai/questions/${questionId}/approve/`),

  // 拒绝AI生成的题目
  rejectQuestion: (questionId: string) =>
    post<any>(`/api/ai/questions/${questionId}/reject/`),

  // 标记题目为已使用
  markQuestionAsUsed: (questionId: string) =>
    post<any>(`/api/ai/questions/${questionId}/mark-used/`),

  // 获取AI模型性能指标
  getModelMetrics: (params?: {
    model_name?: string
    date_from?: string
    date_to?: string
  }) => get<any>('/api/ai/model-metrics/', params)
}